import * as api from '../service/api'


const state = {
  
}

const getters = {

}

const actions = {

	//公共的更新action，必须传fun, cb, err
	async update ({ }, { fun, cb, err, requestType, ...p }) {
		var type = requestType == 'get' ? 'GET' : 'POST';
		let res = await api.go(p, api[fun], type);
		console.log(res.data);
		if(res.errcode === 0){
			if(cb) cb(res.data);
		}else{
			if(err) err(res.errmsg);
		}
	},



	//公共的查询action，必须传fun, cb, err, st
	//约定  st 默认成 set + fun（fun的首字母大写）mutations
	async query ({ commit }, { fun, cb, err, st = 'set' + fun.substring(0,1).toUpperCase() + fun.substring(1), requestType, ...p }) {
		var type = requestType == 'get' ? 'GET' : 'POST';
		let res = await api.go(p, api[fun], type);
		console.log(res.data);
		if(res.errcode === 0){
			commit(st, res.data);
			if(cb) cb(res.data);
		}else{
			if(err) err(res.errmsg);
		}
	},
}

const mutations = {

}

export default {
  state,
  getters,
  actions,
  mutations
}
